package com.mall.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.mall.entity.Coupon;
import com.mall.entity.UserCoupon;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * <p>
 * 用户领取的优惠券表 Mapper 接口
 * </p>
 *
 * @author sppxpp
 * @since 2023-04-16
 */
@Repository
@Mapper
public interface UserCouponMapper extends BaseMapper<UserCoupon> {

    /**
     * 查询为过期优惠券
     * @return list
     */
    @Select("select * from mall_coupon c left join user_coupon uc on c.coupon_id = uc.coupon_id where uc.coupon_status = #{flag} and uc.user_id = #{userId} and uc.is_delete = false and coupon_expire_time > NOW()")
    List<Coupon> queryCoupon(Boolean flag, String userId);

    /**
     * 查询为过期优惠券数量
     * @return list
     */
    @Select("select count(1) from mall_coupon c left join user_coupon uc on c.coupon_id = uc.coupon_id where uc.coupon_status = false and uc.user_id = #{userId} and uc.is_delete = false and coupon_expire_time > NOW()")
    Integer queryCouponCount(String userId);

    /**
     * @description: 查询未使用已经过期的优惠券
     */
    @Select("select * from mall_coupon c left join user_coupon uc on c.coupon_id = uc.coupon_id where uc.coupon_status = false and uc.user_id = #{userId} and uc.is_delete = false and coupon_expire_time < NOW()")
    List<Coupon> queryExpired(String userId);

}
